<template>
  <div>
    <el-card>
      <el-form :model="queryParams" ref="queryForm" v-show="showSearch" label-width="80" :inline="true">
            <el-form-item label="任务名称：" prop="title">
              <el-input v-model="queryParams.title" placeholder="请输入任务名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="接收时间：" label-width="80">
              <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="流程类别：" prop="category"  v-if="!queryParams.taskDefKey">
              <el-select v-model="queryParams.category" clearable filterable>
                <el-option v-for="dict in flow_type" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
            <el-button type="success" @click="handleQuery">搜索</el-button>
            <el-button class="business resetBtn" @click="resetQuery">重置</el-button>
          </el-form-item>
      </el-form>

      <el-row>
        <el-row>
          <p class="pinformation">待办任务列表</p>
          <p class="totalStyle">共 {{ total }} 条数据</p>
        </el-row>
      </el-row>

      <el-table v-loading="loading" :data="todoList" ref="mainTableRef" border>
        <el-table-column label="任务名称" align="center" prop="title" :show-overflow-tooltip="true" />
        <el-table-column label="流程类别" align="center" prop="categoryName" width="180" />
        <el-table-column label="任务节点" align="center" prop="taskName" width="180" />
        <el-table-column label="流程发起人" align="center" width="140">
          <template #default="scope">
            <label>{{ scope.row.startUserName }}</label>
          </template>
        </el-table-column>
        <el-table-column label="接收时间" align="center" prop="createTime" width="180" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
          <template #default="scope">
            <el-button link @click="handleProcess(scope.row)">处理 </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" v-model:pageSizes="pageSizes"/>
    </el-card>
    <checkDialog ref="checkRef"></checkDialog>
    <newBuiltTab ref="writeRef"></newBuiltTab>
  </div>
</template>

<script>
import {
  todoList,
  complete,
  returnList,
  returnTask,
  rejectTask,
  getDeployment,
  delDeployment,
  exportDeployment,
} from "@/api/flowable/todo";
import useRouteView from "@/store/modules/routeView";
import checkDialog from "@/views/lab/labTask/checkDialog";
import newBuiltTab from "@/views/lab/labMethod/newBuilt";
import { getDicts } from "@/api/system/dict/data";

export default {
  name: "Todo",
  components: {
    checkDialog,
    newBuiltTab
  },
  data() {
    return {
      routeViewStore: useRouteView(),
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 自定义高度
      heightParams: [{ tableName: "main", bottomHeight: 21 }],
      // 总条数
      total: 0,
      dateRange: [],
      // 流程待办任务表格数据
      todoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        name: null,
        category: null,
      },
      pageSizes:[50, 100, 150, 200],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      flow_type: []
    };
  },
  created() {
    if (Object.keys(this.$route.query).length > 0 && this.$route.query.taskDefKey) {
      if(this.$route.query.category){
        this.queryParams.category = this.$route.query.category;
      }
      this.queryParams.taskDefKey = this.$route.query.taskDefKey;
      const { path, name, meta } = this.$route;
      this.routeViewStore.setRouteView1({ path, name, meta });
    } 
    this.getList();
    getDicts("flow_type").then((res) => {
      this.flow_type = res.data;
      this.flow_type.splice(0,0,{dictValue:'lab_3',dictLabel:'现场填报'},{dictValue:'lab_9',dictLabel:'实验室填报'});
    });
  },

  watch: {
    $route(newVal, oldVal) {
      if (newVal.path == "/flowable/task/record/index") {
        const { path, name, meta } = newVal;
        this.routeViewStore.setRouteView1({ path, name, meta });
      }
    },
  },

  mounted() {
    this.getTableHeight();
    window.addEventListener("resize", this.getTableHeight);
  },
  beforeUnmount() {
    window.removeEventListener("resize", this.getTableHeight);
  },

  watch: {
    total(newValue, oldValue) {
      if (newValue > 0) {
        this.heightParams = [{ tableName: "main", bottomHeight: 73 }];
      } else {
        this.heightParams = [{ tableName: "main", bottomHeight: 21 }];
      }
      this.getTableHeight();
    },
  },

  methods: {
    /** 查询流程定义列表 */
    getList() {
      this.loading = true;
      let params = this.addDateRange(this.queryParams, this.dateRange);
      todoList(params).then(
        (response) => {
          this.todoList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 跳转到处理页面
    handleProcess(row) {
      debugger;
      if (this.routeViewStore.routeView1) {
        this.$tab.closePage(this.routeViewStore.routeView1);
      }
      let pathStr;
      if (row.moduleCode == -1) {
        if (row.formKey) {
          pathStr = row.formKey;
        } else {
          pathStr = "/flowable/task/record/index";
        }
      } else if (row.moduleCode == 9) {
        if (row.executionId == 0) {
          this.$refs.writeRef.lMId = row.taskId;
          this.$refs.writeRef.myId = row.procInsId;
          this.$refs.writeRef.tabsView();
        } else {
          this.$refs.checkRef.handleCheck(row.procInsId);
        }
        return;
      } else {
        pathStr = row.formKey;
      }

      this.$router.push({
        path: pathStr,
        query: {
          procInsId: row.procInsId,
          executionId: row.executionId,
          deployId: row.deployId,
          taskId: row.taskId,
          finished: true,
          path: "/task/todo",
        },
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        category: null,
        key: null,
        tenantId: null,
        deployTime: null,
        derivedFrom: null,
        derivedFromRoot: null,
        parentDeploymentId: null,
        engineVersion: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加流程定义";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
      getDeployment(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改流程定义";
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有流程定义数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportDeployment(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    getTableHeight() {
      nextTick(() => {
        this.adaptiveHeight(this, this.heightParams);
      });
    },
  },
};
</script>
